var express = require("express");
var router = express.Router();
var optionService = require("../../service/blog/optioninfoService");
const authMiddleware = require("../../middleware/authMiddleware");
var moment = require("moment");
//获取
router.get(
    "/get",
    async (req, res, next) => {
        let list = await optionService.query(req.query);
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//多条件查询（字典）info:[]
router.post(
    "/getOr",
    async (req, res, next) => {
        let typeArr = req.body.type.split(",");
        let list = await optionService.queryOr(typeArr);
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

//修改
router.post(
    "/update",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        const { operateType } = req.body;
        if (operateType !== "root" && operateType !== "values") return res.json({ code: 0, msg: "operateType错误" });

        if (operateType === "root") {
            let { _id, name, type, remark } = req.body;
            await optionService.update({ _id, name, type, remark, updateTime: moment().format("YYYY-MM-DD HH:mm:ss") });
        }
        if (operateType === "values") {
            let { _id, vid, label, value,remark } = req.body;
            await optionService.updateValues({ _id, vid, label, value, remark,updateTime: moment().format("YYYY-MM-DD HH:mm:ss") });
        }
        res.json({ code: 1, msg: "更新成功" });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

//新增root
router.post(
    "/add",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        try {
            if (req.body.operateType === "root") {
                let data = { name: req.body.name, type: req.body.type, remark: req.body.remark };
                var list = await optionService.query({ type: data.type });
                if (list.length > 0) return res.json({ code: 0, msg: "字典类型已存在" });
                else {
                    const result = await optionService.add({ ...data, createTime: moment().format("YYYY-MM-DD HH:mm:ss"), updateTime: moment().format("YYYY-MM-DD HH:mm:ss") });
                    return res.json({ code: 1, msg: "新增成功" });
                }
            }

            if (req.body.operateType === "values") {
                let data = { _id: req.body._id, label: req.body.label, value: req.body.value, remark: req.body.remark, createTime: moment().format("YYYY-MM-DD HH:mm:ss"), updateTime: moment().format("YYYY-MM-DD HH:mm:ss") };
                const result = await optionService.addValues({ ...data });
                return res.json({ code: 1, msg: "新增成功" });
            }
        } catch (error) {
            res.json({ code: 0, msg: error.message });
        }
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

//删除
router.post(
    "/remove",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        const { operateType, _id, vid } = req.body;
        let result = null;
        if (operateType === "root") result = await optionService.delete({ _id });
        if (operateType === "values") result = await optionService.deleteValues({ _id, vid });
        if (result.deletedCount == 1 || result.nModified == 1) res.json({ code: 1, msg: "删除成功" });
        else res.json({ code: 0, msg: "删除失败" });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

module.exports = router;
